package com.mcb.autopublish.demos.web.controller;

import com.mcb.autopublish.demos.web.util.WeChatVerifyServer;
import org.springframework.web.bind.annotation.*;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;

/**
 * <p>
 *
 * </p>
 *
 * @author mcb
 * @since 2024/6/6
 */
@RestController
public class WechatSignatureVerifierApplication {

    // 处理微信服务器的验证请求
    @GetMapping("")
    @ResponseBody
    public String verifySignature(@RequestParam String signature,
                                  @RequestParam String timestamp,
                                  @RequestParam String nonce,
                                  @RequestParam String echostr) {
        // 验证签名是否有效
        if (WeChatVerifyServer.verifySignature(signature, timestamp, nonce)) {
            // 如果签名有效，返回echostr参数值
            return echostr;
        } else {
            // 如果签名无效，返回错误信息
            System.out.println("Signature verification failed.");
            return "Signature verification failed.";
        }

    }







}
